package com.example.uploadfilestoserver;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class UploadFile extends Thread {
	private Context context;
	// 要上传的文件路径，理论上可以传输任何文件，实际使用时根据需要处理
	private String srcPath = "/mnt/sdcard/kate/renren.mp4";
	// 服务器上接收文件的处理页面，这里根据需要换成自己的
	private String actionUrl = "http://pilatus.d1.comp.nus.edu.sg/~a0120575/upload.php";
    
	public UploadFile(Context context, String srcPath,
			String actionUrl) {
		super();
		this.context = context;
		this.srcPath = srcPath;
		this.actionUrl = actionUrl;
	}

	public UploadFile(Context context) {
		super();
		this.context = context;
	}

	@Override
	public void run() {
		
		 String end = "\r\n";  
		    String twoHyphens = "--";  
		    String boundary = "******";  
		    try  
		    {  
		      URL url = new URL(actionUrl);  
		      HttpURLConnection httpURLConnection = (HttpURLConnection) url  
		          .openConnection();  
		      // 设置每次传输的流大小，可以有效防止手机因为内存不足崩溃  
		      // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。  
		      // httpURLConnection.setChunkedStreamingMode(1024 * 1024*50);// 1M*50  
		      // 允许输入输出流  
		      httpURLConnection.setDoInput(true);  
		      httpURLConnection.setDoOutput(true);  
		      httpURLConnection.setUseCaches(false); 
		      // 使用POST方法  
		      httpURLConnection.setRequestMethod("POST");  
		      httpURLConnection.setRequestProperty("Connection", "Keep-Alive");  
		      httpURLConnection.setRequestProperty("ENCTYPE", "multipart/form-data");  
		      httpURLConnection.setRequestProperty("Content-Type",  
		          "multipart/form-data;boundary=" + boundary);  
		      httpURLConnection.setRequestProperty("file", srcPath);
		      DataOutputStream dos = new DataOutputStream(  
		          httpURLConnection.getOutputStream());  
		      dos.writeBytes(twoHyphens + boundary + end);  
		      dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""  
		          + srcPath.substring(srcPath.lastIndexOf("/") + 1)  
		          + "\""  
		          + end);  
		      dos.writeBytes(end);  
		  
		      FileInputStream fis = new FileInputStream(srcPath);
		      System.out.println("FileInputStream:"+fis.available());
		      byte[] buffer = new byte[512*1024]; //512K   
		      int count = 0;  
		      // 读取文件  
		      while ((count = fis.read(buffer)) != -1)  
		      {  
		        dos.write(buffer, 0, count);  
		      }  
		      fis.close();  
		  
		      dos.writeBytes(end);  
		      dos.writeBytes(twoHyphens + boundary + twoHyphens + end);  
		      dos.flush();  
		      
		      int responseCode=httpURLConnection.getResponseCode();
		      System.out.println("ResponseCode:"+responseCode);
		     /* InputStream is = httpURLConnection.getInputStream();  
		      InputStreamReader isr = new InputStreamReader(is, "utf-8");  
		      BufferedReader br = new BufferedReader(isr);  
		      String result = br.readLine();  
		  
		      Toast.makeText(context, result, Toast.LENGTH_LONG).show(); 
		       is.close();*/ 
		      dos.close();  
		      Toast.makeText(context, "Upload Compelete", Toast.LENGTH_LONG).show(); 
		      System.out.println("Upload Complete!");
		    } catch (Exception e)  
		    {  
		      e.printStackTrace();  
		    }  
		    
	}

	};


